﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Logic;
using LFloatMath.Collision2D;
using LFloatMath.Math;
using NetProtocol.POD;
using UnityEditor;
using UnityEngine;
using Random = UnityEngine.Random;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class BattleLogWindow : EditorWindow
    {
        private static BattleLogWindow _instance;

        public static BattleLogWindow Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = CreateWindow<BattleLogWindow>("BattleLog");
                }
                return _instance;
            }
        }

        [MenuItem("GameEditor/BattleDebug/BattleLogWindow", false, 100)]
        public static void Open()
        {
            Instance.Show();
        }

        private void OnInspectorUpdate()
        {
            Repaint();
        }

        private bool[] show = new bool[3] { false, true, false };

        private Vector2[] scroll = new Vector2[3];

        private void OnGUI()
        {
            this.show[0] = EditorGUILayout.Foldout(this.show[0], "表现序列");
            EditorGUILayout.Space(10);
            if (this.show[0])
            {
                EditorGUILayout.BeginVertical();
                this.show[2] = EditorGUILayout.Foldout(this.show[2], "当前表现队列");
                var rQueue = BattleManager.Instance.ctrl?.queueCtrl;
                if (this.show[2] && rQueue != null)
                {
                    if (rQueue.curCmd != null)
                    {
                        EditorGUILayout.LabelField("当前执行中");
                        EditorGUILayout.LabelField(rQueue.curCmd.ToString());
                    }
                    if (rQueue.Queue != null)
                    {
                        EditorGUILayout.Space(10);
                        EditorGUILayout.LabelField("等待执行");
                        scroll[2] = EditorGUILayout.BeginScrollView(scroll[2]);
                        foreach (var rView in rQueue.Queue)
                        {
                            EditorGUILayout.LabelField(rView.ToString());
                        }
                        EditorGUILayout.EndScrollView();
                    }
                }
                ShowLog("已表现序列", ref this.scroll[0], ref EditorLog.battleCommandLog);
                EditorGUILayout.EndVertical();
            }
            this.show[1] = EditorGUILayout.Foldout(this.show[1], "伤害");
            EditorGUILayout.Space(10);
            if (this.show[1])
            {
                EditorGUILayout.BeginVertical();
                ShowLog("伤害", ref this.scroll[1], ref EditorLog.battleDamageLog);
                EditorGUILayout.EndVertical();
            }
        }

        // editor窗口中显示日志，必须再ongui中调用
        private static void ShowLog(string title, ref Vector2 scroll, ref StringBuilder logBuilder)
        {
            EditorGUILayout.LabelField(title);
            if (GUILayout.Button("清理"))
            {
                logBuilder?.Clear();
            }
            scroll = EditorGUILayout.BeginScrollView(scroll);
            EditorGUILayout.Space(10);
            GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
            if (logBuilder == null)
            {
                logBuilder = new StringBuilder();
            }
            var rStr = logBuilder?.ToString() ?? "";
            float height = labelStyle.CalcHeight(new GUIContent(rStr), EditorGUIUtility.currentViewWidth);
            Rect rect = GUILayoutUtility.GetRect(new GUIContent(rStr), labelStyle, GUILayout.Height(height));
            labelStyle.richText = true;
            EditorGUI.LabelField(rect, rStr, labelStyle);
            EditorGUILayout.Space(10);
            EditorGUILayout.EndScrollView();
        }
    }
}